package com.zhuiyun.pm.sys.homePage.controller;

import com.zhuiyun.pm.result.CommonResult;
import com.zhuiyun.pm.sys.homePage.entity.SysChartEntity;
import com.zhuiyun.pm.sys.homePage.entity.dto.BarChartDTO;
import com.zhuiyun.pm.sys.homePage.entity.dto.PieChartDTO;
import com.zhuiyun.pm.sys.homePage.service.HomePageService;
import com.zhuiyun.pm.sys.role.entity.SysRoleEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: HKY
 * @date: 2023年03月21日 16:25
 * @ClassName: HomePageController
 * @Description: 主页数据统计
 */
@RestController
@RequestMapping("/HomePage")
@CrossOrigin
public class HomePageController {


    @Autowired
    HomePageService homePageService;
    /**
     *
     * @author: HKY
     * @date: 2023/3/21 16:55
     * @return com.zhuiyun.pm.result.CommonResult
     * @Description: 数据显示
     */
    @RequestMapping("/getChartList")
    public CommonResult getChartList(){
        List<SysChartEntity> chartList   = homePageService.getChartList();
        return CommonResult.ok(chartList);
    }
    /**
     *
     * @author: HKY
     * @date: 2023/3/21 16:55
     * @return com.zhuiyun.pm.result.CommonResult
     * @Description: 删除图表
     */
    @RequestMapping("/deleteChartByID/{id}")
    public CommonResult deleteChartByID(@PathVariable String id){
        int i = homePageService.deleteByID(id);
        if (i>0){
            return CommonResult.ok();
        }
        return CommonResult.error("500");
    }

    /**
     *
     * @author: HKY
     * @date: 2023/3/21 16:55
     * @return com.zhuiyun.pm.result.CommonResult
     * @Description: 修改图表
     */
    @RequestMapping("/updateChart")
    public CommonResult updateChart(@RequestBody SysChartEntity sysChartEntity){
        int i = homePageService.updateChart(sysChartEntity);
        if (i>0){
            return CommonResult.ok();
        }
        return CommonResult.error("500");
    }

    /**
     *
     * @author: HKY
     * @date: 2023/3/21 16:55
     * @return com.zhuiyun.pm.result.CommonResult
     * @Description: 柱状图OR折线图
     */
    @RequestMapping("/getBarChart/{sqlStr}/{chartType}/{operationType}")
    public CommonResult getBarChart(@PathVariable String sqlStr,@PathVariable String chartType,@PathVariable String operationType){
        Map<String, Object> barChart = homePageService.getBarChart(sqlStr,chartType,operationType);
        return CommonResult.ok(barChart);
    }
    /**
     *
     * @author: HKY
     * @date: 2023/3/21 16:55
     * @return com.zhuiyun.pm.result.CommonResult
     * @Description: 饼状图
     */
    @RequestMapping("/getPieChart/{sqlStr}/{operationType}")
    public CommonResult getPieChart(@PathVariable String sqlStr,@PathVariable String operationType){
        List<PieChartDTO> pieChart = homePageService.getPieChart(sqlStr,operationType);
        return CommonResult.ok(pieChart);
    }
    /**
     *
     * @author: HKY
     * @date: 2023/3/21 16:55
     * @return com.zhuiyun.pm.result.CommonResult
     * @Description: 漏斗图
     */
    @RequestMapping("/getFunnelChart/{sqlStr}/{operationType}")
    public CommonResult getFunnelChart(@PathVariable String sqlStr,@PathVariable String operationType){
        HashMap<String, Object> pieChart = homePageService.getFunnelChart(sqlStr,operationType);
        return CommonResult.ok(pieChart);
    }

}
